/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef LIB2D_GUI_OBJECT_H
#define LIB2D_GUI_OBJECT_H

#include <list>
#include <NoCopy.h>
#include <lib2d/math.h>
#include <Atom.h>
#include <AutoPtr.h>
#include "view/Object.h"

namespace liba
{
namespace lib2d
{
namespace gui
{

class Object : private Nocopy
{
	typedef std::list< Object * > Children;
	typedef Children::iterator iterator;
	typedef Children::reverse_iterator reverse_iterator;

	friend class view::Object;
public:
	explicit Object( Object * parent = 0, const Rect & window_rect = Rect( 0, 0, 0, 0 ) );
	virtual ~Object();

	virtual void life_cycle( double delta_time );
	virtual void on_mouse_out();
	virtual void on_button_down( const Vector2 & point, const Atom & button_name );
	virtual void on_button_up( const Vector2 & point, const Atom & button_name );
	virtual void on_mouse_move_outside( const Vector2 & point );
	virtual void on_mouse_move( const Vector2 & point );
	virtual view::Object * get_view()const;

	virtual void on_text( const std::wstring & text );
	virtual void on_key_down( const std::string & key_name );

	virtual void on_focus( bool focus );
	void change_parent( Object * new_parent );
	iterator insert( Object * obj );
	void erase( iterator it );

	const Rect & get_window_rect()const
	{
		return window_rect;
	}
	const Rect & get_client_rect()const
	{
		return client_rect;
	}
	Object * get_parent()const
	{
		return parent;
	}
	virtual void layout();
private:
	Children children;
	Object * parent;
	iterator parent_it;

	Object * last_move;

	iterator begin()
	{
		return children.begin();
	}
	iterator end()
	{
		return children.end();
	}
protected:
	Rect window_rect;
	Rect client_rect;
	Object * focus_object;
	bool in_focus;
	bool mouse_pressed;

	AutoPtr< view::Object > view;
};

} //namespace gui
} //namespace lib2d
} //namespace liba

using namespace liba;

#endif //LIB2D_GUI_OBJECT_H
